<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of WriteOff
 *
 * @author Administrator
 */
class Zcsq extends WMS_Controller
{

    var $whsCode;
    var $users;
    function __construct()
    {
        parent::__construct();
        $gp = gp();
        $userData = $this->session->userdata('users');
        $this->whsCode = $userData['user_main_whs_code'];
        $this->users=$userData;
        //加载模型
        $this->load->service('wms/zcsq_order_service');
        $this->load->service('wms/api_service');
        $this->load->service('wms/time_service');
        $this->load->service('wms/inventory_service');
        $this->load->service('wms/in_out_type_service');
        $this->load->model('/wms/api/get_product_info_from_erp');
    }

    public function show()
    {
        //锁死仓库
        $whs = $this->whsCode;
        $wherein = array('whs_code' => $whs);
        $where = array();
        $gp = gp();
        $order_id = $gp['order_id'] ? $gp['order_id'] : '';
        $page = $gp['page'] ? intval($gp['page']) : 1;
        //
        $str = '?';
        $limit = 15;
        if ('' != $order_id)
        {
            $where['order_id'] = $order_id;
            $str .= 'order_id=' . $order_id;
        }
        //
        //分页查询
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url('wms/Zcsq/show' . $str);
        $config['total_rows'] = $this->zcsq_order_service->get_list_count($where, $wherein, $like);
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $data['pageShow'] = $this->pagination->create_links();
        //分页结束
        //获取产品库存列表
        $orderstr='created_time desc';
        $receivingList = $this->zcsq_order_service->get_list_page(($page - 1) * $limit, $limit, $orderstr, $where, $wherein, $like);
        $data['receivingList'] = $this->api_service->fields_erp_product_info($receivingList);
        $data['receivingList'] = $this->time_service->show_time($data['receivingList']);
        $data['status_view'] = $this->zcsq_order_service->get_status();
        $in_out_type_rs = $this->in_out_type_service->get_all();
        $in_out_type = array();
        foreach ($in_out_type_rs as $type)
        {
            $in_out_type[$type['key']] = $type['key'] . '-' . $type['name_cn'];
        }
        foreach ($data['receivingList'] as $key => $value)
        {
            $data['receivingList'][$key]['in_out_type'] = $in_out_type[$value['in_out_type']];
        }
        $this->load->view('defaults/wms/zcsq_order_list', $data);
        $this->load->view('defaults/common/footer', $data);
    }

    function detail($order_id='')
    {
        $data = $this->zcsq_order_service->get_with_items($order_id);
        $data['items'] = $this->get_product_info_from_erp->fields_product_info($data['items']);
        $in_out_type_list = $this->in_out_type_service->get_all();
        $in_out_type_array = array_combine(array_column($in_out_type_list, "key"), array_column($in_out_type_list, "name_cn"));
        if ($in_out_type_array[$data['in_out_type']])
        {
            $data['in_out_type'] = $in_out_type_array[$data['in_out_type']];
        }
        $data['status_view'] = $this->zcsq_order_service->get_status();
        $data['created_time']=date("Y-m-d H:i:s",$data['created_time']);
        $this->load->view('defaults/wms/zcsq_order_panel', $data);
        $this->load->view('defaults/common/footer');
    }

    function zcsq_agree_t($order_id)
    {
        // if($this->users['id']!=191 and  $this->users['id']!=172 and  $this->users['id']!=133  and $this->users['id']!=153){
        //     js_goto(getenv("HTTP_REFERER"), "权限不足");
        //      exit;
        // }
        header("Content-type: text/html; charset=utf-8");
        $order_data = $this->zcsq_order_service->get_with_items($order_id);
        if (1 != $order_data['status'])
        {
            js_goto(getenv("HTTP_REFERER"), lang('wms_tips_20023'));
            exit;
        }
        //创建出入库数组
        $items = $order_data['items'];
        $skus_in = array();
        foreach ($items as $item)
        {
            $sku = $item['sku'];
            $whs = $order_data['whs_code'];
            $fromWhsCode = $order_data['from_whs_code'];
            $quantity = $item['quantity'];
            $refNum = $order_id;
            $comments = $order_data['comments'];
            $in_out_type = $order_data['in_out_type'];
            $operator = $order_data['creator_id'];
            $sku_info = array(
                'sku' => $sku,
                'whsCode' => $whs,
                'fromWhsCode' => $fromWhsCode,
                'quantity' => $quantity,
                'refNum' => $refNum,
                'comments' => $comments,
                'in_out_type' => $in_out_type,
                'operator' => $operator,
            );
            array_push($skus_in, $sku_info);
        }
        //入库申请
        if ('in' == $order_data['in_or_out'])
        {
            $success = $this->inventory_service->stock_array_t($skus_in);
            if ($success)
            {
                $isupdata = $this->zcsq_order_service->update_zcsq_status($order_id, $status = 2);
            }
            else
            {
                js_goto(getenv("HTTP_REFERER"), lang('wms_tips_20016'));
                exit;
            }
        }
        elseif ('out' == $order_data['in_or_out'])
        {

            $success = $this->inventory_service->out_stock_array_t($skus_in);
            if ($success)
            {
                $this->zcsq_order_service->update_zcsq_status($order_id, $status = 2);
            }
            else
            {
                js_goto(getenv("HTTP_REFERER"), lang('wms_tips_20016'));
                exit;
            }
        }
        elseif ('out_in' == $order_data['in_or_out'])
        {
            $success = $this->inventory_service->out_in_stock_array_t($skus_in);
            if ($success)
            {
                $this->zcsq_order_service->update_zcsq_status($order_id, $status = 2);
            }
            else
            {
                js_goto(getenv("HTTP_REFERER"), lang('wms_tips_20016'));
                exit;
            }
        }
        else
        {
            js_goto(getenv("HTTP_REFERER"), 'Request error');
            exit;
        }
        $this->detail($order_id);
    }

    function zcsq_cancel($order_id)
    {
        // if($this->users['id']!=191 and  $this->users['id']!=172 and  $this->users['id']!=133 and $this->users['id']!=153){
        //     js_goto(getenv("HTTP_REFERER"), "权限不足");
        //     exit;
        // }
        $this->zcsq_order_service->update_zcsq_status($order_id, $status = 3);
        $this->detail($order_id);
    }

    function zcsq_cg_panel()
    {
        $data['whsCode'] = $this->whsCode;
        $this->load->view('defaults/wms/zcsq_cg_panel', $data);
        $this->load->view('defaults/common/footer');
    }

}

?>
